package code1.binary_tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 原题链接：https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
 */
public class P530 {

    //二叉搜索树的最小绝对差

    private List<Integer> list=new ArrayList<>();

    //把二叉搜素树变成对应的数组
    public void buildArray(TreeNode root){
        if(root==null){
            return;
        }
        buildArray(root.left);
        list.add(root.val);
        buildArray(root.right);
    }

    public int getMinimumDifference(TreeNode root) {
        buildArray(root);
        int minDif=Integer.MAX_VALUE;
        //遍历数组，找最小绝对差值
        for(int i=0;i<list.size()-1;i++){
            int num=list.get(i+1)-list.get(i);
            minDif=Math.min(num,minDif);
        }
        return minDif;
    }
}
